package com.example.zwh.computershop;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;

import com.example.zwh.computershop.androidUtil.ActivityCollector;
import com.example.zwh.computershop.classify.Classify_Fragment;
import com.example.zwh.computershop.handleDataClient.handleData;
import com.example.zwh.computershop.home.Home_Fragment;
import com.example.zwh.computershop.login.Login;
import com.example.zwh.computershop.my_shopp.MyShopp_Fragment;
import com.example.zwh.computershop.shopping_cart.ShoppCar_Fragment;
import com.example.zwh.computershop.sqlModel.Product;

import java.util.ArrayList;
import java.util.List;

public class Home_Activity extends FragmentActivity implements View.OnClickListener{
    private Intent intent;

    private ViewPager mViewPager;

    private Myapp myapp = null;

    private FragmentAdapter mFragmentAdapter;
    List<Fragment> flist = new ArrayList<>();

    //4个按钮
    private ImageButton imgBtn_home;//首页
    private ImageButton imgBtn_classify;//分类
    private ImageButton imgBtn_shoppCar;//购物车
    private ImageButton imgBtn_myshopp;//我的


    //4个fragment
    private Home_Fragment homeFragment;//首页fragment
    private Classify_Fragment classifyFragment;//分类
    private ShoppCar_Fragment shoppCarFragment;//购物车
    private MyShopp_Fragment myShoppFragment;//我的


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ActivityCollector.addActivity(this);
        initView();
        initEvent();
        initData();
    }

    void initView() {
        myapp = (Myapp) getApplication();

        imgBtn_home = (ImageButton) findViewById(R.id.imgBtn_home);
        imgBtn_classify = (ImageButton) findViewById(R.id.imgBtn_classify);
        imgBtn_shoppCar = (ImageButton) findViewById(R.id.imgBtn_shoppcar);
        imgBtn_myshopp = (ImageButton) findViewById(R.id.imgBtn_myshopp);

        mViewPager = (ViewPager)findViewById(R.id.id_viewpager);

        homeFragment = new Home_Fragment();//首页fragment
        classifyFragment = new Classify_Fragment();//分类
        shoppCarFragment = new ShoppCar_Fragment();//购物车
        myShoppFragment = new MyShopp_Fragment();//我的

        flist.add(homeFragment);
        flist.add(classifyFragment);
        if (myapp.getLoState() != 0) {
            flist.add(shoppCarFragment);
            flist.add(myShoppFragment);
        }
        mFragmentAdapter = new FragmentAdapter(this.getSupportFragmentManager(), flist);

        //为ViewPager设置adapter
        mViewPager.setAdapter(mFragmentAdapter);
        //判断加载的首页
        int num = 0;
        num = getIntent().getIntExtra("select",0);
        if(num == 2) {
            setTabDefaultColor();
            imgBtn_shoppCar.setImageResource(R.drawable.tab_shopping_pressed);
            mViewPager.setCurrentItem(num);
        }
        else {
            mViewPager.setCurrentItem(0);
        }
    }

    void initData() {
        //获取产品数据
        Thread t = new Thread(new takeThread());
        t.start();
        try {
            //等待线程返回
            t.join();
        }catch (InterruptedException ie) {
            ie.printStackTrace();
        }
    }

    class takeThread extends Thread {
        @Override
        public void run() {
            //如果不为空证明已经取过数据了
            if (myapp.getProducts().size() != 0) return;
            ArrayList<Product> products = new ArrayList<>();
            handleData.getPro(products);
            myapp.setProducts(products);
        }
    }

    void initEvent() {
        imgBtn_home.setOnClickListener(this);
        imgBtn_classify.setOnClickListener(this);
        imgBtn_shoppCar.setOnClickListener(this);
        imgBtn_myshopp.setOnClickListener(this);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {//当viewpager滑动时，对应的底部导航按钮的图片要变化
                int currentItem = mViewPager.getCurrentItem();
                setTabDefaultColor();
                switch (currentItem) {
                    case 0:
                        imgBtn_home.setImageResource(R.drawable.tab_home_pressed);
                        break;
                    case 1:
                        imgBtn_classify.setImageResource(R.drawable.fx_tab_fenlei_pressed);
                        break;
                    case 2:
                        if(myapp.getLoState() == 0) {
                            intent = new Intent();
                            intent.setClass(Home_Activity.this, Login.class);
                            startActivity(intent);
                        }else {
                            imgBtn_shoppCar.setImageResource(R.drawable.tab_shopping_pressed);
                        }
                        break;
                    case 3:
                        if(myapp.getLoState() == 0) {
                            intent = new Intent();
                            intent.setClass(Home_Activity.this, Login.class);
                            startActivity(intent);
                        }else {
                            imgBtn_myshopp.setImageResource(R.drawable.fx_tab_my_pressed);
                        }
                        break;
                    default:
                        break;
                }
            }
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                //暂时没用
                if (arg0 == 1 && myapp.getLoState() == 0 && (arg1 > 0 || arg2 > 0)) {
                    intent = new Intent();
                    intent.setClass(Home_Activity.this, Login.class);
                    startActivity(intent);
                }
            }
            @Override
            public void onPageScrollStateChanged(int arg0) {}
        });
    }

    //恢复底部菜单图片
    private void setTabDefaultColor() {
        imgBtn_home.setImageResource(R.drawable.tab_home_normal);
        imgBtn_classify.setImageResource(R.drawable.fx_tab_fenlei_normal);
        imgBtn_shoppCar.setImageResource(R.drawable.tab_shopping_normal);
        imgBtn_myshopp.setImageResource(R.drawable.fx_tab_my_normal);
    }

    @Override
    public void onClick(View v) {
        setTabDefaultColor();
        switch (v.getId()) {
            case R.id.imgBtn_home:
                imgBtn_home.setImageResource(R.drawable.tab_home_pressed);
                mViewPager.setCurrentItem(0);
                break;
            case R.id.imgBtn_classify:
                imgBtn_classify.setImageResource(R.drawable.fx_tab_fenlei_pressed);
                mViewPager.setCurrentItem(1);
                break;
            case R.id.imgBtn_shoppcar:
                if(myapp.getLoState() == 0) {
                    intent = new Intent();
                    intent.setClass(Home_Activity.this, Login.class);
                    startActivity(intent);
                }else {
                    imgBtn_shoppCar.setImageResource(R.drawable.tab_shopping_pressed);
                    mViewPager.setCurrentItem(2);
                }
                break;
            case R.id.imgBtn_myshopp:
                if(myapp.getLoState() == 0) {
                    intent = new Intent();
                    intent.setClass(Home_Activity.this, Login.class);
                    startActivity(intent);
                }else {
                    imgBtn_myshopp.setImageResource(R.drawable.fx_tab_my_pressed);
                    mViewPager.setCurrentItem(3);
                }
                break;
        }
    }
}
